package spring.cloud.eurekaclientproduct.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.cloud.eurekaclientproduct.service.R4jFacade;
import com.spring.cloud.commonutils.vo.UserInfo;
import com.spring.cloud.commonutils.vo.ResultMessage;

import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

/**
 *  Resilience4j 测试 控制层接口
 */
@RestController
@RequestMapping("/r4j")
public class R4jController {

    @Autowired
    private R4jFacade r4jFacade;

    /**
     * circuitBreaker断路器接口
     * @param msg
     * @return
     */
    @GetMapping("/exp/{msg}")
    public ResultMessage exp(@PathVariable("msg") String msg){
        return r4jFacade.exp(msg);
    }

    /**
     * RateLimiter限速器接口
     * @return
     */
    @GetMapping("/users")
    public List<UserInfo> getUserInfo(){

        Long start  = System.currentTimeMillis();;
        List<UserInfo> userList  = new ArrayList<>();
        Long id = 0L;

        while (true){
            id++;
            UserInfo user = r4jFacade.getUser(id);
            userList.add(user);
            Long end = System.currentTimeMillis();

            if(end - start >= 1000){
                break;
            }
        }
        return userList;
    }

    /**
     * Bulkhead 舱壁隔离接口
     * @return
     */
    @GetMapping("/users2")
    public List<UserInfo> getUserInfo2(){

        Long start  = System.currentTimeMillis();;
        List<UserInfo> userList  = new ArrayList<>();
        Long id = 0L;

        while (true){
            id++;
            UserInfo user = r4jFacade.getUser3(id);
            userList.add(user);
            Long end = System.currentTimeMillis();

            if(end - start >= 1000){
                break;
            }
        }
        return userList;
    }

    /**
     * Retry重试接口
     * @return
     */
    @GetMapping("/exp")
    public ResultMessage exp(){
        return r4jFacade.exp();
    }

    /**
     * 缓存测试接口
     */
    @GetMapping("/users/cache")
    public List<UserInfo> getCacheUserInfo(){

        List<UserInfo> userList  = new ArrayList<>();
        Long id = 0L;
        for(int i=0;i<10;i++){

            UserInfo user = r4jFacade.cacheUserInfo(id);
            userList.add(user);

        }
        return userList;
    }

    /**
     * 超时限制器 接口
     * @return
     */
    @GetMapping("/timeout")
    public ResultMessage timeout(){

        return r4jFacade.timeout();
    }

    /**
     * 混合使用组件 接口
     * @return
     */
    @GetMapping("/mixUserInfo")
    public List<UserInfo> mixUserInfo(){
        Long id = 0L;

        List<UserInfo> list = new ArrayList<>();
        for(int i = 0;i<5;i++){
            list.add(r4jFacade.mixUserInfo(id));
        }
        return list;
    }


}
